#include "../include/server.h"

void * func(void * p)
{
	pthread_detach(pthread_self());
	sqlite3 * db = ((parg_t *)p)->pg_mydb;
	int sockfd = ((parg_t *)p)->pg_sockfd;

	char name[20] = {0};
	char flag[5] = {0};
	char *errmsg, **resultp;
	int nr,nc;

	prol_t temp = {0};
	int ret;

	temp.pl_type = 10;

	strcpy(temp.info_t.ctldata.ct_arg1,"Welcom!\n");
	strcpy(temp.info_t.ctldata.ct_arg2,"login or register?\n");
	send(sockfd,&temp,sizeof temp,0);
	while(1)
	{
		ret = recv(sockfd,&temp,sizeof temp,0);
	printf("recvtype = %d\n",temp.pl_type);
	printf("recv: %d\n", ret);
		if(0 == ret)
			break;

		if(2 == temp.pl_type)
			ret = server_register(&temp,db);

		else if(1 == temp.pl_type)
		{
			ret = server_login(&temp,db,name,flag);
		}
		else
		{
				server_error(&temp);
		}

		if(-1 == ret)
		{
			temp.pl_type = 10;
			strcpy(temp.info_t.ctldata.ct_arg1,"sqlite3 error\n");
			strcpy(temp.info_t.ctldata.ct_arg2,"stop server!\n");
			send(sockfd,&temp,sizeof temp,0);
			return;
		}

		send(sockfd,&temp,sizeof temp,0);
		if(1 == ret)
		{
			break;
		}

	}
	while(1)
	{
		ret = recv(sockfd,&temp,sizeof temp,0);
		if(0 == ret)
			return;
		printf("recvtype = %d\n",temp.pl_type);
	printf("recv: %d\n", ret);
	update_data(db,name,flag);
		if(0 == ret)
			break;
		switch(temp.pl_type)
		{
			case 3:
				ret = server_promote(&temp,db,name,sockfd,flag);
				break;
			case 4:
				ret = server_find(&temp,db,name,sockfd,flag);
				break;
			case 5:
				ret = server_chpasswd(&temp,db,name,sockfd,flag);
				break;
			case 6:
				ret = server_delete(&temp,db,name,sockfd,flag);
				break;
			case 7:
				ret = server_delself(&temp,db,name,sockfd,flag);
				send(sockfd,&temp,sizeof temp,0);
				return 0;
			case 8:
				temp.pl_type = 10;
				strcpy(temp.info_t.ctldata.ct_arg1,"server unlinked!\n");
				strcpy(temp.info_t.ctldata.ct_arg2,"");
				send(sockfd,&temp,sizeof temp,0);
				return 0;
			default:
				server_error(&temp);
		}
		if(-1 == ret)
		{
			temp.pl_type = 10;
			strcpy(temp.info_t.ctldata.ct_arg1,"sqlite3 error\n");
			strcpy(temp.info_t.ctldata.ct_arg2,"stop server!\n");
			send(sockfd,&temp,sizeof temp,0);
			return;
		}
		printf("sendtype = %d\n",temp.pl_type);
		printf("send: %d\n", send(sockfd,&temp,sizeof temp,0));
	}
}

int server_register(prol_t * temp,sqlite3 * db)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	sprintf(buf,"select * from usrpass where name='%s'",temp->info_t.ctldata.ct_arg1);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		printf("error: %s\n",errmsg);
		return -1;
	}

	if(0 == nr)
	{
		//插入新注册用户
		sprintf(buf,"insert into usrpass values('%s','%s','%s','%s','5000','0')",\
				temp->info_t.ctldata.ct_arg1,\
				temp->info_t.ctldata.ct_arg2,\
				temp->info_t.ctldata.ct_arg3,\
				temp->info_t.ctldata.ct_arg4);
		if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
		{
			printf("error: %s\n",errmsg);
			return -1;
		}


		temp->pl_type = 10;
		strcpy(temp->info_t.ctldata.ct_arg1,"register sucess!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}
	else
	{
		temp->pl_type = 10;
		strcpy(temp->info_t.ctldata.ct_arg1,"existed name!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}
}

int server_login(prol_t * temp,sqlite3 * db,char * name,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	temp->pl_type = 10;
	sprintf(buf,"select passwd,flag from usrpass where name='%s'",temp->info_t.ctldata.ct_arg1);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		printf("error: %s\n",errmsg);
		return -1;
	}

	if(0 == nr)
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"no this ID!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}
	else if(0 == strcmp(*(2 + resultp),temp->info_t.ctldata.ct_arg2))
	{
		strcpy(name,temp->info_t.ctldata.ct_arg1);
		strcpy(flag,*(resultp+3));

		strcpy(temp->info_t.ctldata.ct_arg1,"login sucess!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 1;
	}
	else
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"wrong passwd\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}
}

void server_error(prol_t * temp)
{
		temp->pl_type = 10;
		strcpy(temp->info_t.ctldata.ct_arg1,"illegar input!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
}

int server_promote(prol_t * temp,sqlite3 * db,char *name,int sockfd,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	temp->pl_type = 10;

	//判断权限
	
	if(0 != strncmp("2",flag, 1))
	{
		printf("flag = %s\n",flag);
		strcpy(temp->info_t.ctldata.ct_arg1,"can't change ");
		strcpy(temp->info_t.ctldata.ct_arg2,"no authority!\n");
		return 0;
	}
	
	//判断用户是否存在
	sprintf(buf,"select * from usrpass where name='%s'",temp->info_t.ctldata.ct_arg1);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		printf("error: %s\n",errmsg);
		return -1;
	}

	if(0 == nr)
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"no this ID!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}

	//修改权限
	sprintf(buf,"update usrpass set %s='%s' where name='%s'",\
			temp->info_t.ctldata.ct_arg2,\
			temp->info_t.ctldata.ct_arg3,\
			temp->info_t.ctldata.ct_arg1);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"database error!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}


	strcpy(temp->info_t.ctldata.ct_arg1,"modify sucess!\n");
	strcpy(temp->info_t.ctldata.ct_arg2,"");
	return 1;
}
int server_find(prol_t * temp,sqlite3 * db,char *name,int sockfd,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	temp->pl_type = 10;

	//判断权限
	if(0 == strcmp("0",flag))
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"can't change ");
		strcpy(temp->info_t.ctldata.ct_arg2,"no authority!\n");
		return 0;
	}
	
	//普通员工执行
	if(0 == strcmp("1",flag))
	{
		sprintf(buf,"select age,phone from usrpass where name='%s'",temp->info_t.ctldata.ct_arg1);
		if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
		{
			printf("error: %s\n",errmsg);
			return -1;
		}

		sprintf(buf,"%s: age:%s phone:%s\n",temp->info_t.ctldata.ct_arg1,*(resultp + 2),*(resultp + 3));
		strcpy(temp->info_t.ctldata.ct_arg1,buf);
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}
	//管理员执行
	if(0 == strcmp("2",flag))
	{
		sprintf(buf,"select passwd,age,phone,salary,flag from usrpass where name='%s'",temp->info_t.ctldata.ct_arg1);
		if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
		{
			printf("error: %s\n",errmsg);
			return -1;
		}

		sprintf(buf,"%s: passwd:%s age:%s phone:%s salary:%s flag:%s\n",temp->info_t.ctldata.ct_arg1,*(resultp + 5),\
				*(resultp + 6),\
				*(resultp + 7),\
				*(resultp + 8),\
				*(resultp + 9));
		strcpy(temp->info_t.ctldata.ct_arg1,buf);
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}
}
int server_chpasswd(prol_t * temp,sqlite3 * db,char *name,int sockfd,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	temp->pl_type = 10;

	sprintf(buf,"update usrpass set passwd='%s' where name='%s'",\
			temp->info_t.ctldata.ct_arg1,\
			name);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"database error!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}


	strcpy(temp->info_t.ctldata.ct_arg1,"passwd modify sucess!\n");
	strcpy(temp->info_t.ctldata.ct_arg2,"");
	return 1;

}
int server_delete(prol_t * temp,sqlite3 * db,char *name,int sockfd,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	temp->pl_type = 10;

	//判断权限
	if(0 != strcmp("2",flag))
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"can't delete ");
		strcpy(temp->info_t.ctldata.ct_arg2,"no authority!\n");
		return 0;
	}

	//执行
	sprintf(buf,"delete from usrpass where name='%s'",temp->info_t.ctldata.ct_arg1);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"database error!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}


	strcpy(temp->info_t.ctldata.ct_arg1,"delete sucess!\n");
	strcpy(temp->info_t.ctldata.ct_arg2,"");
	return 0;
}
int server_delself(prol_t * temp,sqlite3 * db,char *name,int sockfd,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];
	temp->pl_type = 10;

	//执行
	sprintf(buf,"delete from usrpass where name='%s'",name);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		strcpy(temp->info_t.ctldata.ct_arg1,"database error!\n");
		strcpy(temp->info_t.ctldata.ct_arg2,"");
		return 0;
	}


	strcpy(temp->info_t.ctldata.ct_arg1,"delete sucess!\n");
	strcpy(temp->info_t.ctldata.ct_arg2,"");
	return 0;
}

void update_data(sqlite3 * db,char * name,char * flag)
{
	char *errmsg, **resultp;
	int nr,nc;
	char buf[100];

	sprintf(buf,"select flag from usrpass where name='%s'",name);
	if(sqlite3_get_table(db,buf,&resultp,&nr,&nc,&errmsg) != SQLITE_OK)
	{
		printf("error: %s\n",errmsg);
		return ;
	}

	strcpy(flag,*(resultp + 1));
}
